package demo1;

public class LeetCode1658 {
    public int minOperations(int[] nums, int x) {
        int n = nums.length , len = -1 ,sum = 0;
        int tmp = sum(nums);
        int ret = tmp - x;
        if(ret < 0) return -1;
        for(int left = 0 , right = 0; right < n; right++) {
            sum += nums[right];
            while(sum > ret) sum -= nums[left++];
            if(sum == ret) len = Math.max(len , right - left + 1);
        }
        return len == -1 ? -1 : n - len;
    }
    private int sum(int[] arr) {
        int sum = 0;
        for(int i : arr) {
            sum += i;
        }
        return sum;
    }
}
